import customParseFormat from 'dayjs/plugin/customParseFormat'
import dayjs from 'dayjs'

dayjs.extend(customParseFormat)
/**
 * 格式化日期
 */
export const formatDate = (date: any, pattern: string = 'YYYY-MM-DD HH:mm:ss') => {
  if (!date) {
    return ''
  }
  return dayjs(date).format(pattern)
}

/**
 * 获取最近几天的范围
 */
export const getLastTimeRange = (count: number) => {
  // 拼接时间
  const time1 = new Date()
  const time2 = new Date()
  if (count === 1) {
    time1.setTime(time1.getTime() - 24 * 60 * 60 * 1000)
  } else {
    if (count >= 0) {
      time1.setTime(time1.getTime())
    } else {
      if (count === -2) {
        time1.setTime(time1.getTime() + 24 * 60 * 60 * 1000 * 2)
      } else {
        time1.setTime(time1.getTime() + 24 * 60 * 60 * 1000)
      }
    }
  }

  const Y1 = time1.getFullYear()
  const M1 = time1.getMonth() + 1 > 9 ? time1.getMonth() + 1 : '0' + (time1.getMonth() + 1)
  const D1 = time1.getDate() > 9 ? time1.getDate() : '0' + time1.getDate()
  const timer1 = Y1 + '-' + M1 + '-' + D1 + ' ' + '23:59:59' // 当前时间

  time2.setTime(time2.getTime() - 24 * 60 * 60 * 1000 * count)
  const Y2 = time2.getFullYear()
  const M2 = time2.getMonth() + 1 > 9 ? time2.getMonth() + 1 : '0' + (time2.getMonth() + 1)
  const D2 = time2.getDate() > 9 ? time2.getDate() : '0' + time2.getDate()
  const timer2 = Y2 + '-' + M2 + '-' + D2 + ' ' + '00:00:00' // 之前的7天或者30天
  return [timer2, timer1]
}

/**
 * 判断日期是否在日期范围内
 */
export const judgeDateInDateRange = (
  rangeDate: (string | number | Date)[],
  date: string | number | Date
) => {
  const startDateTimeStamp = new Date(rangeDate[0]).getTime()
  const endDateTimeStamp = new Date(rangeDate[1]).getTime()
  const dateTimeStamp = new Date(date).getTime()
  return dateTimeStamp >= startDateTimeStamp && dateTimeStamp <= endDateTimeStamp
}

/**
 * 获取当前时间
 */
export const getCurrentTime = () => {
  // 拼接时间
  const time = new Date()
  const Y1 = time.getFullYear()
  const M1 = time.getMonth() + 1 > 9 ? time.getMonth() + 1 : '0' + (time.getMonth() + 1)
  const D1 = time.getDate() > 9 ? time.getDate() : '0' + time.getDate()
  // 当前时间
  return Y1 + '-' + M1 + '-' + D1
}

/**
 * 获取时间戳
 */
export const getTimeStamp = (date: string | number | Date, len = 10) => {
  if (len === 10) {
    return new Date(date).getTime() / 1000
  } else {
    return new Date(date).getTime()
  }
}

/**
 * 获取时间相差多少
 */
export const getFormatTimeDiff = (
  startTime: number,
  endTime: number,
  formatType: any,
  precision = 0
) => {
  if (endTime - startTime < 0) {
    return 0
  }
  switch (formatType) {
    case 'second':
      return ((endTime - startTime) / 1000 / 60).toFixed(precision) + '秒'
    case 'hour':
      return ((endTime - startTime) / 1000 / 60 / 60).toFixed(precision) + '小时'
    case 'day':
      return ((endTime - startTime) / 1000 / 60 / 60 / 24).toFixed(precision) + '天'
    default:
      return ((endTime - startTime) / 1000).toFixed(precision) + '毫秒'
  }
}

// 获取日期所在周的周几日期
// export const getWeekdayDate = (date, week) => {
//   const checkDate = new Date(date)
//   const time = new Date(date).setDate(checkDate.getDate() + week - (checkDate.getDay()))
//   return HUtils.dateUtils.dateFormat(time, 'yyyy-MM-dd hh:mm:ss')
// }
